package com.ja.openchat.views;

import android.content.Context;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ja.openchat.R;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import org.jetbrains.annotations.NotNull;

public class ChooseReadTimeDialogNew extends BottomPopupView {
    private OnChooseReadTimeCallback mCallback;

    private TextView read_time_title;
    private SeekBar seekBar;
    private Switch seekBar_open_readTime;
    private int currentProgress = 5;

    public static void show(Context context, OnChooseReadTimeCallback callback){
        new XPopup.Builder(context)
                .asCustom(new ChooseReadTimeDialogNew(context,callback))
                .show();
    }

    public ChooseReadTimeDialogNew(@NonNull @NotNull Context context, OnChooseReadTimeCallback callback) {
        super(context);
        mCallback = callback;
    }



    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_choose_chat_read_time_new;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
         read_time_title = findViewById(R.id.read_time_title);
        seekBar = findViewById(R.id.seekBar);
        seekBar_open_readTime = findViewById(R.id.seekBar_open_readTime);
        initSwitchBtn();
        seekBar_open_readTime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                initSwitchBtn();
            }
        });
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                read_time_title.setText(String.format("消息阅读%s秒后自动销毁",progress));
                currentProgress = progress;
                if(mCallback!=null){
                    mCallback.onSuccess(0,currentProgress+"秒",currentProgress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void initSwitchBtn() {
        if(!seekBar_open_readTime.isChecked()){
            seekBar.setThumb(null);
            read_time_title.setText("阅后即焚倒计时关闭");
        }else{
            seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_thumb));
            read_time_title.setText("消息阅读5秒后自动销毁");
        }
    }

    @Override
    protected int getPopupWidth() {
        return XPopupUtils.getAppWidth(getContext());
    }

}
